package com.yanqu.road.server.cmd.wingroom;

import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.entity.wingroom.WingRoomRegion;
import com.yanqu.road.pb.wingroom.WingRoomProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.wingroom.WingRoomModule;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.wingroom.WingRoomMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.Map;

/**
 * @author ：zfr
 * @date ：2022/3/17 10:18
 */
@Cmd(code = GameProtocol.S_WING_ROOM_GET_OTHER_PLAN, desc = "拷贝别人的装修方案")

public class CopyOtherPlayerPlanCmd extends AbstractCommand {
    private long otherPlayerServerId;
    private long otherPlayerUserId;
    private long otherPlayerPlanUid;
    private String planName;
    private WingRoomProto.GetOtherPlanReqMsg reqMsg;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = WingRoomProto.GetOtherPlanReqMsg.parseFrom(packet.getBytes());
        otherPlayerServerId = reqMsg.getOtherPlayerServerId();
        otherPlayerUserId = reqMsg.getOtherPlayerUserId();
        planName = reqMsg.getPlanName();
        if (!StringUtils.isNumber(reqMsg.getPlanUid())) {
            ret = GameErrorCode.E_WING_ROOM_COPY_PLAN_KEY_ERROR;
            return true;
        }
        otherPlayerPlanUid = Long.parseLong(reqMsg.getPlanUid());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        WingRoomProto.GetOtherPlanRespMsg.Builder respMsg = WingRoomProto.GetOtherPlanRespMsg.newBuilder();
        if (otherPlayerServerId == player.getUserId()) {
            respMsg.setRet(GameErrorCode.E_WING_ROOM_COPY_SELF_PLAN);
            player.sendPacket(ClientProtocol.U_WING_ROOM_GET_OTHER_PLAN, respMsg);
        }


        ret = player.getModule(WingRoomModule.class).checkPlanNameValid(planName);
        if (ret != 0) {
            respMsg.setRet(ret);
            player.sendPacket(ClientProtocol.U_WING_ROOM_GET_OTHER_PLAN, respMsg);
            return;
        }
        //同服
        if (otherPlayerServerId == GameServer.getInstance().getServerId()) {
            respMsg.setServerId(otherPlayerServerId);
            Map<Integer, WingRoomRegion> planRegion = WingRoomMgr.copyPlan(otherPlayerUserId, otherPlayerPlanUid, respMsg);
            if (planRegion == null) {
                respMsg.setRet(GameErrorCode.E_WING_ROOM_COPY_PLAN_NO_FOUND);
                player.sendPacket(ClientProtocol.U_WING_ROOM_GET_OTHER_PLAN, respMsg);
                return;
            }
            player.getModule(WingRoomModule.class).copyOtherPlayerPlan(planRegion, planName, respMsg);
            respMsg.setRet(0);
            player.sendPacket(ClientProtocol.U_WING_ROOM_GET_OTHER_PLAN, respMsg);
            return;
        }
        ServerInfo serverInfo = ServerListMgr.getServerInfo(otherPlayerServerId);
        if (serverInfo == null || !serverInfo.isOpen()) {
            respMsg.setRet(GameErrorCode.E_WING_ROOM_COPY_PLAN_NO_FOUND);
            player.sendPacket(ClientProtocol.U_WING_ROOM_GET_OTHER_PLAN, respMsg);
            return;
        }
        //请求跨服
        player.sendPacket(CrossProtocol.C_WING_ROOM_COPY_OTHER_PLAN, reqMsg.toBuilder());
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
